package com.comdo.newup_loan.module.mine;

import android.os.AsyncTask;

import com.comdo.newup_loan.comom.Config;
import com.comdo.newup_loan.http.RetrofitHelper;
import com.comdo.newup_loan.model.Result;
import com.comdo.newup_loan.model.UserInfoBean;
import com.comdo.newup_loan.util.BitmapUtil;
import com.comdo.newup_loan.util.StringUtils;
import com.google.gson.Gson;

import java.io.File;
import java.util.HashMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by liyuan on 2017/10/30.
 */

class MinePresenter extends MineContract.Presenter{
    @Override
    public void getUserInfo(int userId) {
        Gson gson=new Gson();
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("userId",userId);
        map.put("token", Config.loginInfo.getToken());
        map.put("sign", StringUtils.SortStringAndAddKey(map));
        String strEntity = gson.toJson(map);
        RequestBody body = RequestBody.create(okhttp3.MediaType.parse("application/json;charset=UTF-8"),strEntity);
        mCompositeDisposable.add(RetrofitHelper.createMyApi().getUserInfo(body)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Result<UserInfoBean>>() {
                    @Override
                    public void accept(Result<UserInfoBean> Result) throws Exception {
                        if(Result.getCode()==1){
                           mView.getUserInfo(Result.getResult());
                        }else if(Result.getCode()==-3){
                            mView.exitLogin();
                            mView.getFail("您的登陆已经失效，请重新登陆");
                        }else{
                            mView.getFail(Result.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {

                    }
                }));
    }
    @Override
    public void upLoad(final int id, final String filePath) {
        new AsyncTask<Integer, Integer, File>() {
            @Override
            protected File doInBackground(Integer... params) {
                //压缩图片
                File file = new File(BitmapUtil.compressImage(filePath));
                return file;
            }

            @Override
            protected void onPostExecute(File file) {
                super.onPostExecute(file);
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("userId", id);
                map.put("token", Config.loginInfo.getToken());

                RequestBody requestFile =
                        RequestBody.create(MediaType.parse("multipart/form-data"), file);
                MultipartBody.Part fileBody =
                        MultipartBody.Part.createFormData("files[]", file.getName(), requestFile);
                MultipartBody.Part userId =
                        MultipartBody.Part.createFormData("userId", String.valueOf(id));
                MultipartBody.Part token =
                        MultipartBody.Part.createFormData("token", Config.loginInfo.getToken());
                MultipartBody.Part sign =
                        MultipartBody.Part.createFormData("sign", StringUtils.SortStringAndAddKey(map));
                mCompositeDisposable.add(RetrofitHelper.createMyApi().headUpload(fileBody, userId, token, sign)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Result<String>>() {
                            @Override
                            public void accept(Result<String> Result) throws Exception {
                                if (Result.getCode() == 1) {
                                    mView.upLoad(Result.getResult());
                                } else {
                                    mView.getFail(Result.getMessage());
                                }
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                mView.getFail("请求失败");
                            }
                        }));
            }
        }.execute();

    }
}
